package com.tensquare.search.service;

import com.tensquare.entity.PageResult;
import com.tensquare.search.dao.ArticleSearchDao;
import com.tensquare.search.model.Article;
import com.tensquare.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleSearchService {

    @Autowired
    private ArticleSearchDao searchDao ;

    @Autowired
    private IdWorker idWorker ;

    public void deleteArticleById(String id) {
        searchDao.deleteById(id);
    }

    public void updateArticle(String id, Article article) {
        article.setId(id);
        searchDao.save(article);
    }

    public void saveArticle(Article article) {
        article.setId(idWorker.nextId().toString());
        searchDao.save(article);
    }

    public Article findArticleById(String id) {
        return searchDao.findById(id).get();
    }

    public List<Article> findAllArticle() {


        return null ;
    }

    public Page<Article> findByKey(String key, Integer page, Integer size) {
        Pageable condition = PageRequest.of(page - 1, size);
        return searchDao.findByTitleOrContentLike(key, key, condition);
    }

}
